<template>
  <div class="login_container">
    <div class="background"></div>
    <div class="title">海康视频监控平台</div>
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.svg" alt="">
      </div>
      <el-form
          class="login_form"
          ref="formRef"
          style="max-width: 600px"
          :model="formState"
          label-width="auto">
        <el-form-item
            prop="username"
            :rules="[{required: true,message: '请输入用户名',trigger: 'blur'}]">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="formState.username"/>
        </el-form-item>

        <el-form-item
            prop="password"
            :rules="[{required: true,message: '请输入密码',trigger: 'blur'}]">
          <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" show-password v-model="formState.password"/>
        </el-form-item>

        <el-form-item>
          <el-button style="width: 100%" :disabled="disabled" @click="onFinish" type="primary">登录</el-button>
        </el-form-item>
        <div>
          <el-button style="float: right" type="primary" @click="router.push('/register')" link>
            没有账户，去注册
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup name="LoginView" lang="ts">
import {Lock, User} from '@element-plus/icons-vue'
import {computed, reactive} from 'vue';
import {useRouter} from 'vue-router';
import axios from "@/axios/api";
import {ElMessage} from "element-plus";
import type Result from "@/model/Result";

interface FormState {
  username: string;
  password: string;
}

const login = (form: FormState) => {
  return axios.post<Result<string>>(`/api/v1/user/login`, form);
};
const formState = reactive<FormState>({
  username: '',
  password: ''
});

const disabled = computed(() => {
  return !(formState.username && formState.password);
});

const router = useRouter()
const onFinish = () => {
  login(formState).then((resp) => {
        localStorage.setItem('token', resp.data.data || '');
        ElMessage.success("登录成功");
        router.push("/home/welcome");
      }
  )
};

</script>


<style lang="less" scoped>
.background {
  position: fixed;
  min-width: 100%;
  min-height: 100%;
  background: url("@/assets/login_back.jpg");
  z-index: -1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-size: cover;
}

.title {
  text-align: center;
  width: 100%;
  margin-top: 150px;
  font-size: 50px;
  color: #032198;
}

.login_box {
  width: 400px;
  height: 300px;
  padding: 10px 0px;
  background-color: rgba(221, 221, 221, 0.5);
  border-radius: 3px;
  position: absolute; //移动
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%); //移动到正中心
  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eeeeee; //边框
    border-radius: 50%; //圆角
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff;
    box-shadow: 0 0 10px #dddddd; //给边框加阴影
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%; //圆角
      background-color: #eeeeee;
    }
  }
}

.btns {
  display: flex;
  justify-content: center; //尾部对齐
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px !important;
  box-sizing: border-box;
}
</style>